package com.fg.cloud.factory.controller.factory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.ExpressModel;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.ExpressCostsEntity;
import com.fg.cloud.manager.entry.factory.ExpressModelEntity;
import com.fg.cloud.manager.service.factory.ExpressModelService;

/**
 * Description:快递费模板Controller
 * 
 * @author shaoqiping
 * @date 2017年9月18日上午11:04:02
 */
@Controller
@RequestMapping("/expressModel")
public class ExpressModelController extends BaseController {

	@Autowired
	private ExpressModelService modelService;

	/**
	 * Description:新增快递费模板
	 * 
	 * @param request
	 * @param response
	 * @param modelEntity
	 * @author shaoqiping
	 * @date 2017年9月18日上午11:46:13
	 */
	@RequestMapping(value = "/insertModel", method = RequestMethod.POST)
	@ResponseBody
	public void insertModel(HttpServletRequest request, HttpServletResponse response, ExpressModelEntity modelEntity,
			String ems, String express, String postage) {
		try {
			if (modelEntity == null || StringUtils.isEmpty(modelEntity.getName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请填写相关必填数据");
				return;
			}
			if (modelService.findByName(modelEntity.getName()) != null) {
				doResult(response, false, StatusCode.LACKVALUE, null, "快递费模板名称重复");
				return;
			}
			List<ExpressCostsEntity> costs = new ArrayList<ExpressCostsEntity>();
			if(ems!=null&&!StringUtils.isEmpty(ems)){
				costs.addAll(JSONArray.parseArray(ems, ExpressCostsEntity.class));
			}
			if(express!=null&&!StringUtils.isEmpty(express)){
				costs.addAll(JSONArray.parseArray(express, ExpressCostsEntity.class));
			}
			if(postage!=null&&!StringUtils.isEmpty(postage)){
				costs.addAll(JSONArray.parseArray(postage, ExpressCostsEntity.class));
			}
			setCreateInfo(modelEntity, request);
			setUpdateInfo(modelEntity, request);
			modelEntity.setFacId(getCurrentFactoryUser().getFacId());
			boolean flag = modelService.insertSelective(modelEntity, costs);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, "快递费模板新增成功");
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, "快递费模板新增失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:查看详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年11月1日上午9:37:10
	 */
	@RequestMapping(value = "/findDetails", method = RequestMethod.POST)
	@ResponseBody
	public void findDetails(HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应模板");
				return;
			}
			ExpressModelEntity temp = modelService.find(id);
			List<ExpressCostsEntity> ems = new ArrayList<>();
			List<ExpressCostsEntity> express = new ArrayList<>();
			List<ExpressCostsEntity> postage = new ArrayList<>();
			List<ExpressCostsEntity> costs = temp.getCosts();
			for (ExpressCostsEntity entity : costs) {
				if (entity.getType() == ExpressModel.EXPRESS) {
					express.add(entity);
				} else if (entity.getType() == ExpressModel.EMS) {
					ems.add(entity);
				} else {
					postage.add(entity);
				}
			}
			temp.setCosts(null);
			Collections.reverse(ems);
			Collections.reverse(express);
			Collections.reverse(postage);
			Map<String, Object> map = new HashMap<>();
			map.put("model", temp);
			map.put("ems",ems);
			map.put("express", express);
			map.put("postage", postage);
			doResult(response, true, StatusCode.FINDSUCCESS, map, "查询成功");

		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description：更新快递费模板
	 * 
	 * @param request
	 * @param response
	 * @param modelEntity
	 * @author shaoqiping
	 * @date 2017年9月18日上午11:46:26
	 */
	@RequestMapping(value = "/updateModel", method = RequestMethod.POST)
	@ResponseBody
	public void updateModel(HttpServletRequest request, HttpServletResponse response, ExpressModelEntity modelEntity,
			String ems, String express, String postage) {
		try {
			if (modelEntity == null || LongUtils.isEmpty(modelEntity.getId())
					|| StringUtils.isEmpty(modelEntity.getName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请填写相关必填数据");
				return;
			}
			ExpressModelEntity temp = modelService.findByName(modelEntity.getName());
			if (temp != null && !temp.getId().equals(modelEntity.getId())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "快递费模板名称重复");
				return;
			}
			List<ExpressCostsEntity> costs = new ArrayList<ExpressCostsEntity>();
			if(ems!=null&&!StringUtils.isEmpty(ems)){
				costs.addAll(JSONArray.parseArray(ems, ExpressCostsEntity.class));
			}
			if(express!=null&&!StringUtils.isEmpty(express)){
				costs.addAll(JSONArray.parseArray(express, ExpressCostsEntity.class));
			}
			if(postage!=null&&!StringUtils.isEmpty(postage)){
				costs.addAll(JSONArray.parseArray(postage, ExpressCostsEntity.class));
			}
			modelEntity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(modelEntity, request);
			boolean flag = modelService.update(modelEntity, costs);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, "快递费模板修改成功");
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, "快递费模板修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:删除快递费模板
	 * 
	 * @param request
	 * @param response
	 * @param modelEntity
	 * @author shaoqiping
	 * @date 2017年9月18日上午11:59:43
	 */
	@RequestMapping(value = "/deleteModel", method = RequestMethod.POST)
	@ResponseBody
	public void deleteModel(HttpServletRequest request, HttpServletResponse response, ExpressModelEntity modelEntity) {
		try {
			if (modelEntity == null || LongUtils.isEmpty(modelEntity.getId())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要删除的快递费模板");
				return;
			}
			modelEntity.setFacId(getCurrentFactoryUser().getFacId());
			boolean flag = modelService.removeModel(modelEntity);
			if (flag) {
				doResult(response, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	@RequestMapping(value = "/showModels", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void showModels(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText) {
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<ExpressModelEntity> pageInfo = new PageUtils<ExpressModelEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageInfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			pageInfo.setCondition(condition);

			modelService.findList(pageInfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageInfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
